﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DGui
{
    public class Button : ICloneable
    {
        #region Fields and Properties

        bool IsButton = true;
        Color ColorNormal = Color.White;
        Color ColorSelect = new Color(255, 255, 0, 1);

        bool isKeyDown = false;
        bool isPressed = false;

        public Texture2D RegularTexture { get; set; }
        public Texture2D PressedTexture { get; set; }
        public SpriteFont Font { get; set; }
        public Vector2 Position { get; set; }
        public Rectangle Bounds { get; set; }

        string regularTexture;
        string pressedTexture;

        public event EventHandler Click;

        Viewport viewport;
        #endregion

        public Button()
        {

        }

        public Button(string regularTexture, string pressedTexture)
        {
            this.regularTexture = regularTexture;
            this.pressedTexture = pressedTexture;
            this.IsButton = true;

        }

        public Button(string regularTexture, string pressedTexture, bool isBotton)
        {
            this.regularTexture = regularTexture;
            this.pressedTexture = pressedTexture;
            this.IsButton = isBotton;

        }

        public void SetViewport(Viewport viewport)
        {
            this.viewport = viewport;
        }

        public void LoadContent(ContentManager Content, string assetName)
        {
            if (regularTexture != null)
            {
                RegularTexture = Content.Load<Texture2D>(@"Textures\" + assetName + "\\" + regularTexture);
                Bounds = new Rectangle((int)Position.X, (int)Position.Y, RegularTexture.Width, RegularTexture.Height);
            }
            if (IsButton)
            {
                if (pressedTexture != null)
                {
                    PressedTexture = Content.Load<Texture2D>(@"Textures\" + assetName + "\\" + pressedTexture);
                    Bounds = new Rectangle((int)Position.X, (int)Position.Y, PressedTexture.Width, PressedTexture.Height);
                }
            }

        }

        public void Update(GameTime gameTime)
        {
            if (RegularTexture != null)
            {
                MouseState mouseState = Mouse.GetState();
                HandleInput(mouseState);
            }

        }
  
        public void HandleInput(MouseState mouseState)
        {
            bool pressed = false;
            Vector2 position = Vector2.Zero;
            Rectangle rectangleMouse = new Rectangle(mouseState.X, mouseState.Y, 25, 25);

            if (IntersectWith(new Vector2(mouseState.X, mouseState.Y)))
            {
                isPressed = true;
            }
            else
            {
                isPressed = false;
            }
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                pressed = true;
                position = new Vector2(mouseState.X, mouseState.Y);
                if (IntersectWith(new Vector2(mouseState.X, mouseState.Y)))
                {
                    FireClick();

                }
            }
        }
      
        private bool IntersectWith(Vector2 position)
        {
            Rectangle touchTap = new Rectangle((int)position.X - 1, (int)position.Y - 1, 2, 2);
            return Bounds.Intersects(touchTap);
        }
        
        public void FireClick()
        {
            if (Click != null)
            {
                Click(this, EventArgs.Empty);
            }
        }

        public Rectangle GetBounds()
        {
            return Bounds;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            if (IsButton)
            {
                spriteBatch.Draw(isPressed ? PressedTexture : RegularTexture, Bounds, Color.White);
            }
            else
            {
                Color color = isPressed ? ColorSelect : ColorNormal;
                spriteBatch.Draw(RegularTexture, Bounds, color);
            }
            spriteBatch.End();
          
        }
        public object Clone()
        {
            Button buttons = new Button();
            buttons.RegularTexture = RegularTexture;
            buttons.PressedTexture = PressedTexture;
            buttons.Position = Position;
            buttons.Bounds = Bounds;

            return buttons;
        }

    }
}
